/*
 * Contains all the assignment validation rules...
 * */

public class AssignmentValidator {
	private Driver driver;
	private Delivery delivery;
	private boolean conflitHoraire = false;
	private boolean tooManyMinutes = false;
	
	public AssignmentValidator(Driver driver, Delivery deliveryToAdd){
		this.driver = driver;
		this.delivery = deliveryToAdd;
		//computer nbHoursDeliveriesToday & checkConflits
		int nbMinutesDeliveriesToday = 0;
		for(Object object : driver.getDeliveriesAssigned()){
			Delivery delivery = (Delivery) object;
			//Ajouter aller-retour au nombre de minutes d'aujourd'hui!
			nbMinutesDeliveriesToday += 2 * delivery.getEstimatedDeliveryDurationInMinutes();
			//Check conflit horaire!
			if(isConflitHoraire(delivery, deliveryToAdd))
				conflitHoraire = true;
		}
		
		nbMinutesDeliveriesToday += 2 * deliveryToAdd.getEstimatedDeliveryDurationInMinutes();

		if(driver.isJunior()) {
			if(nbMinutesDeliveriesToday > 12*60)
				tooManyMinutes = true;
		}else { //isSenior()
			if(nbMinutesDeliveriesToday > 8*60)
				tooManyMinutes = true;
		}
	}

	public boolean isValidAssignment() {
		return !conflitHoraire && !tooManyMinutes;
	}

	public boolean isConflitHoraire() {
		return conflitHoraire;
	}
	
	public boolean isTooManyHours() {
		return tooManyMinutes; //same..!
	}

	public static boolean isConflitHoraire(Delivery delivery1, Delivery delivery2) {
		int duree1 = 2*delivery1.getEstimatedDeliveryDurationInMinutes(); //aller-retour
		int duree2 = 2*delivery2.getEstimatedDeliveryDurationInMinutes(); //aller-retour
		int start1 = delivery1.getDesiredDeliveryTimeInMinutes();
		int start2 = delivery2.getDesiredDeliveryTimeInMinutes();
		
		if(start1 > start2){
			return start2+duree2 > start1;
		}
		else if(start1 == start2){
			return true;
		}
		else{ //start2 > start1
			return start1+duree1 > start2;
		}
	}
}
